package com.aysafety.iotdataplatformweb.services.elevator.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.aysafety.core.DBDriver.hbase.HBaseDriver;
import com.aysafety.core.common.DateUtils;
import com.aysafety.iotdataplatformweb.services.elevator.ElevatorService;

@Service
public class ElevatorServiceImpl implements ElevatorService {

    private final Map<String, Object> realTimeStructure = new HashMap<String, Object>() {
        {
            put("clientId", "String");
            put("deviceId", "String");
            put("speed", "String");
            put("heightD", "String");
            put("temperature", "String");
            put("humidityD", "String");
            put("floor", "Integer");
            put("door", "Integer");
            put("people", "Integer");
            put("direction", "String");
            put("flatState", "Integer");
            put("realTime", "Date");
        }
    };
    private final Map<String, Object> alarmStructure = new HashMap<String, Object>() {
        {
            put("clientId", "String");
            put("deviceId", "String");
            put("alarmTime", "Date");
            put("faultCode", "String");
            put("faultDesc", "String");
        }
    };

    public long countRealTime(String deviceId, String startTime, String endTime) throws Throwable {
        List<Map<String, Object>> conditions = new ArrayList<Map<String, Object>>();
        Map<String, Object> parram = new HashMap<String, Object>();
        String startRow = "";
        String endRow = "";
        parram.put("columnFamily", "RealTime");
        parram.put("columnId", "deviceId");
        parram.put("valueType", "String");
        parram.put("value", deviceId);
        parram.put("symbol", "EQUAL");
        conditions.add(parram);
        parram = new HashMap<String, Object>();
        parram.put("columnFamily", "RealTime");
        parram.put("columnId", "realTime");
        parram.put("valueType", "Long");
        parram.put("value", DateUtils.fomatDateTime(startTime).getTime());
        parram.put("symbol", "GREATER_OR_EQUAL");
        conditions.add(parram);
        parram = new HashMap<String, Object>();
        parram.put("columnFamily", "RealTime");
        parram.put("columnId", "realTime");
        parram.put("valueType", "Long");
        parram.put("value", DateUtils.fomatDateTime(endTime).getTime());
        parram.put("symbol", "LESS_OR_EQUAL");
        conditions.add(parram);
        if (StringUtils.isNotBlank(startTime)) {
            endRow = String.valueOf(DateUtils.fomatDateTime(startTime).getTime()) + ".";
        }
        if (StringUtils.isNotBlank(endTime)) {
            startRow = String.valueOf(DateUtils.fomatDateTime(endTime).getTime()) + ".";
        }
        return HBaseDriver.countData("ElevatorRealTime", conditions, startRow, endRow);
    }

    public List<Map<String, Object>> queryRealTime(String deviceId, String startTime, String endTime, int page,
        int size, String startRow) throws Throwable {
        List<Map<String, Object>> conditions = new ArrayList<Map<String, Object>>();
        Map<String, Object> parram = new HashMap<String, Object>();
        String endRow = "";
        parram.put("columnFamily", "RealTime");
        parram.put("columnId", "deviceId");
        parram.put("valueType", "String");
        parram.put("value", deviceId);
        parram.put("symbol", "EQUAL");
        conditions.add(parram);
        parram = new HashMap<String, Object>();
        parram.put("columnFamily", "RealTime");
        parram.put("columnId", "realTime");
        parram.put("valueType", "Long");
        parram.put("value", DateUtils.fomatDateTime(startTime).getTime());
        parram.put("symbol", "GREATER_OR_EQUAL");
        conditions.add(parram);
        parram = new HashMap<String, Object>();
        parram.put("columnFamily", "RealTime");
        parram.put("columnId", "realTime");
        parram.put("valueType", "Long");
        parram.put("value", DateUtils.fomatDateTime(endTime).getTime());
        parram.put("symbol", "LESS_OR_EQUAL");
        conditions.add(parram);
        if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime) && StringUtils.isBlank(startRow)) {
            startRow = String.valueOf(DateUtils.fomatDateTime(startTime).getTime()) + ".";
            endRow = String.valueOf(DateUtils.fomatDateTime(endTime).getTime()) + ".";
        }
        return HBaseDriver.queryData("ElevatorRealTime", conditions, page, size, this.realTimeStructure, startRow,
            endRow);
    }

    public long countAlarm(String deviceId, String startTime, String endTime) throws Throwable {
        List<Map<String, Object>> conditions = new ArrayList<Map<String, Object>>();
        Map<String, Object> parram = new HashMap<String, Object>();
        String startRow = "";
        String endRow = "";
        parram.put("columnFamily", "AlarmData");
        parram.put("columnId", "deviceId");
        parram.put("valueType", "String");
        parram.put("value", deviceId);
        parram.put("symbol", "EQUAL");
        conditions.add(parram);
        parram = new HashMap<String, Object>();
        parram.put("columnFamily", "AlarmData");
        parram.put("columnId", "alarmTime");
        parram.put("valueType", "Long");
        parram.put("value", DateUtils.fomatDateTime(startTime).getTime());
        parram.put("symbol", "GREATER_OR_EQUAL");
        conditions.add(parram);
        parram = new HashMap<String, Object>();
        parram.put("columnFamily", "AlarmData");
        parram.put("columnId", "alarmTime");
        parram.put("valueType", "Long");
        parram.put("value", DateUtils.fomatDateTime(endTime).getTime());
        parram.put("symbol", "LESS_OR_EQUAL");
        conditions.add(parram);
        if (StringUtils.isNotBlank(startTime)) {
            endRow = String.valueOf(DateUtils.fomatDateTime(startTime).getTime()) + ".";
        }
        if (StringUtils.isNotBlank(endTime)) {
            startRow = String.valueOf(DateUtils.fomatDateTime(endTime).getTime()) + ".";
        }
        return HBaseDriver.countData("ElevatorRealTime", conditions, startRow, endRow);
    }

    public List<Map<String, Object>> queryAlarm(String deviceId, String startTime, String endTime, int page, int size,
        String startRow) throws Throwable {
        List<Map<String, Object>> conditions = new ArrayList<Map<String, Object>>();
        Map<String, Object> parram = new HashMap<String, Object>();
        String endRow = "";
        parram.put("columnFamily", "AlarmData");
        parram.put("columnId", "deviceId");
        parram.put("valueType", "String");
        parram.put("value", deviceId);
        parram.put("symbol", "EQUAL");
        conditions.add(parram);
        parram = new HashMap<String, Object>();
        parram.put("columnFamily", "AlarmData");
        parram.put("columnId", "alarmTime");
        parram.put("valueType", "Long");
        parram.put("value", DateUtils.fomatDateTime(startTime).getTime());
        parram.put("symbol", "GREATER_OR_EQUAL");
        conditions.add(parram);
        parram = new HashMap<String, Object>();
        parram.put("columnFamily", "AlarmData");
        parram.put("columnId", "alarmTime");
        parram.put("valueType", "Long");
        parram.put("value", DateUtils.fomatDateTime(endTime).getTime());
        parram.put("symbol", "LESS_OR_EQUAL");
        conditions.add(parram);
        if (StringUtils.isNotBlank(startTime) && StringUtils.isBlank(startRow)) {
            endRow = String.valueOf(DateUtils.fomatDateTime(startTime).getTime()) + ".";
        }
        if (StringUtils.isNotBlank(endTime)) {
            startRow = String.valueOf(DateUtils.fomatDateTime(endTime).getTime()) + ".";
        }
        return HBaseDriver.queryData("ElevatorRealTime", conditions, page, size, this.alarmStructure, startRow, endRow);
    }
}
